package show.me.the.money.util.exp.standard;

import java.util.List;

import show.me.the.money.util.context.Context;
import show.me.the.money.util.exp.ExpRunner;
import show.me.the.money.util.exp.ExpSymbols;
import show.me.the.money.util.exp.Expression;

@SuppressWarnings("unchecked")
public class ISNULL extends Expression{
	public ISNULL(){
		symbol = ExpSymbols.NULL;
	}
	
	public String run(List lsExp, Context ctx) {
		Boolean result = true;
		try {
			List la1 = (List)lsExp.get(1);
			String flag = (String)ExpRunner.run(la1, ctx);			
			List la2 = (List)lsExp.get(2);
			Object target = ExpRunner.run(la2, ctx);
			if(flag.equalsIgnoreCase("is")){
				result = target == null;
			}else{
				result = target != null;
			}
		} catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return String.valueOf(result);
	}
	
	public String toString(List lsExp, Context ctx) {
		try {			
			List la1 = (List)lsExp.get(1);
			String flag = (String)ExpRunner.run(la1, ctx);
			List la2 = (List)lsExp.get(2);
			String target = String.valueOf(ExpRunner.toString(la2, ctx));
			StringBuffer sb = new StringBuffer(target);
			if(flag.equalsIgnoreCase("is")){
				sb.append(" is ").append(symbol).toString();
			}else{
				sb.append(" is not ").append(symbol).toString();
			}
			return sb.toString();
		} 
		catch (Exception e) {
			logger.error("toString JSONException:", e);
		}
		return "";
	}

}
